﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Linkman
{
    /// <summary>
    /// AddLinkman 的摘要说明。
    /// </summary>
    public partial class AddLinkmanPage : Page
    {
        private int userid;

        protected override void OnInit(EventArgs e)
        {
            ddl_LinkmanType.SelectedIndexChanged += ddl_LinkmanType_SelectedIndexChanged;
            btn_Back.Click += btn_Back_Click;
            btn_AddList.Click += btn_AddList_Click;
            dgrd_List.PageIndexChanged += dgrd_List_PageIndexChanged;
            dgrd_List.ItemDataBound += dgrd_List_ItemDataBound;
            dgrd_Linkman.PageIndexChanged += dgrd_Linkman_PageIndexChanged;
            dgrd_Linkman.ItemDataBound += dgrd_Linkman_ItemDataBound;
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            userid = SiteContext.Current.UserID;

            if (!Page.IsPostBack)
            {
                tbl_Custom.Visible = false;
                BindGrid((ddl_LinkmanType.SelectedItem.Value == "staff") ? 1 : 2);
                if (ddl_LinkmanType.SelectedItem.Value == "custom")
                {
                    BindTypeList();
                }
            }

            base.OnLoad(e);
        }

        private void BindTypeList()
        {
            using (var dr = MyLinkman.GetCustomLinkmanType())
            {
                dlt_Type.DataSource = dr;
                dlt_Type.DataKeyField = "ID";
                dlt_Type.DataBind();
            }
        }

        //绑定选择框
        private void BindGrid(int type)
        {
            var staff = new Components.Staff();
            var cm = new Components.CM();

            if (type == 1)
            {
                SqlDataReader dr_staff = staff.GetAllStaffs();
                DataTable dt_staff = Tools.ConvertDataReaderToDataTable(dr_staff);
                dt_staff.TableName = "Staff";
                dgrd_List.Visible = true;
                dgrd_Linkman.Visible = false;
                dgrd_List.DataKeyField = "Staff_ID";
                dgrd_List.DataSource = dt_staff.DefaultView;
                dgrd_List.DataBind();
            }
            else if (type == 2)
            {
                SqlDataReader dr_linkman = cm.GetLinkmenByClientAddmanID(userid);
                DataTable dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);
                dt_linkman.TableName = "Linkman";
                dgrd_Linkman.Visible = true;
                dgrd_List.Visible = false;
                dgrd_Linkman.DataKeyField = "ID";
                dgrd_Linkman.DataSource = dt_linkman.DefaultView;
                dgrd_Linkman.DataBind();
            }
        }

        private void ddl_LinkmanType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((ddl_LinkmanType.SelectedItem.Value == "staff") || (ddl_LinkmanType.SelectedItem.Value == "client"))
            {
                tbl_Select.Visible = true;
                tbl_Custom.Visible = false;
                btn_AddList.Visible = true;
                BindGrid((ddl_LinkmanType.SelectedItem.Value == "staff") ? 1 : 2);
            }
            else if (ddl_LinkmanType.SelectedItem.Value == "custom")
            {
                tbl_Select.Visible = false;
                tbl_Custom.Visible = true;
                btn_AddList.Visible = false;
                BindTypeList();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            var mylinkman = new MyLinkman();
            var clinkman = new Components.CustomLinkman();
            clinkman.Name = tbx_Name.Text;
            clinkman.Age = tbx_Age.Text;
            clinkman.Gender = (ddl_Gender.Items[ddl_Gender.SelectedIndex].Value == "1") ? true : false;
            clinkman.UnitAddress = tbx_UnitAddress.Text;
            clinkman.UnitTelephone = tbx_UnitTelephone.Text;
            clinkman.UnitZip = tbx_UnitZip.Text;
            clinkman.FamilyAddress = tbx_FamilyAddress.Text;
            clinkman.FamilyTelephone = tbx_FamilyTelephone.Text;
            clinkman.FamilyZip = tbx_FamilyZip.Text;
            clinkman.Email = tbx_Email.Text;
            clinkman.Mobile = tbx_Mobile.Text;
            clinkman.Memo = tbx_Memo.Text;

            for (int i = 0; i < dlt_Type.Items.Count; i++)
            {
                if (((CheckBox)dlt_Type.Items[i].Controls[1]).Checked)
                {
                    clinkman.Type += dlt_Type.DataKeys[i] + ",";
                }
            }

            mylinkman.AddCustomLinkman(clinkman, userid);

            Response.Write("<script>location.href='ListView.aspx?type=3'</script>");
        }

        private void btn_AddList_Click(object sender, EventArgs e)
        {
            string selectedstring = "";
            int type = 0;
            //取得选中的id值
            if (dgrd_List.Visible)
            {
                type = 1;
                foreach (DataGridItem dgi in dgrd_List.Items) //找到checkbox control
                {
                    for (int i = 0; i < dgi.Cells[0].Controls.Count; i++)
                    {
                        if ((dgi.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox") && ((CheckBox)dgi.Cells[0].Controls[i]).Checked)
                        {
                            selectedstring += dgrd_List.DataKeys[dgi.ItemIndex] + ",";
                        }
                    }
                }
            }
            else if (dgrd_Linkman.Visible)
            {
                type = 2;
                foreach (DataGridItem dgi in dgrd_Linkman.Items) //找到checkbox control
                {
                    for (int i = 0; i < dgi.Cells[0].Controls.Count; i++)
                    {
                        if ((dgi.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox") && ((CheckBox)dgi.Cells[0].Controls[i]).Checked)
                        {
                            selectedstring += dgrd_Linkman.DataKeys[dgi.ItemIndex] + ",";
                        }
                    }
                }
            }

            if (selectedstring.IndexOf(",") != -1)
            {
                selectedstring = selectedstring.Substring(0, selectedstring.Length - 1);
                string[] arrids = selectedstring.Split(',');
                for (int i = 0; i < arrids.Length; i++)
                {
                    var mlinkman = new MyLinkman();
                    mlinkman.AddLinkmanToList(type, Int32.Parse(arrids[i]), userid);
                }
            }
            Response.Write("<script>location.href='ListView.aspx?type=" + type + "';</script>");
        }

        private void btn_Back_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='ListView.aspx'</script>");
        }

        private void dgrd_List_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            var mlinkman = new MyLinkman();
            if (e.Item.ItemIndex != -1)
            {
                if (mlinkman.HaveInList(1, userid, Int32.Parse(((DataGrid)sender).DataKeys[e.Item.ItemIndex].ToString())))
                {
                    for (int i = 0; i < e.Item.Cells[0].Controls.Count; i++)
                    {
                        if (e.Item.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                        {
                            ((CheckBox)(e.Item.Cells[0].Controls[i])).Enabled = false;
                        }
                    }
                }
            }
        }

        private void dgrd_Linkman_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            var mlinkman = new MyLinkman();
            if (e.Item.ItemIndex != -1)
            {
                if (mlinkman.HaveInList(2, userid, Int32.Parse(((DataGrid)sender).DataKeys[e.Item.ItemIndex].ToString())))
                {
                    for (int i = 0; i < e.Item.Cells[0].Controls.Count; i++)
                    {
                        if (e.Item.Cells[0].Controls[i].GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                        {
                            ((CheckBox)(e.Item.Cells[0].Controls[i])).Enabled = false;
                        }
                    }
                }
            }
        }

        private void dgrd_List_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
            BindGrid(1);
        }

        private void dgrd_Linkman_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            ((DataGrid)source).CurrentPageIndex = e.NewPageIndex;
            BindGrid(2);
        }
    }
}